import json
import os
import sys

import requests
from openai import OpenAI
sys.path.append('/JiuTian/swarm/swarm-main/swarm')
from swarm import Swarm,Agent


#测试基本功能
client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url=os.environ["DEEPSEEK_BASE_URL"])
# client = OpenAI(api_key="sk-dmtzbscxyyaijoprsokdsxcjxazjrvrgzxhinvkwgomkonhg", base_url="https://api.siliconflow.cn/v1")

weather_cache = {}

#获取天气相关函数
#  city_en 为城市英文名
def get_weather(city_en):

    print('调用天气API')

    # 从缓存中获取数据
    #city转为小写
    city_en = city_en.lower()
    if city_en in weather_cache:
        print('从缓存中获取数据')
        return weather_cache[city_en]

    weather_api_key = '9e7d36701fac2b6f3da5546a76661377'

    param = {
        'q': city_en,
        'units': 'metric',
        'appid': weather_api_key,
        "lang":"zh_cn"
    }

    url = "https://api.openweathermap.org/data/2.5/weather"

    response = requests.get(url=url, params=param)
    data = response.json()

    # 缓存数据
    weather_cache[city_en] = json.dumps(data)    #缓存数据

    return weather_cache[city_en]


# model_name = "deepseek-ai/DeepSeek-V3"
model_name = "deepseek-chat"
#测试多智能体功能
swarm_client = Swarm(client)
agent_a = Agent(name = "Agent A",instructions= "你是一个乐于助人的助手",functions=[get_weather],model=model_name)

response = swarm_client.run(agent=agent_a,messages=[{"role":"user","content":"今天天津的天气怎么样？"}])
print(response.messages[-1]["content"])